package com.hongye.why.util;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.hongye.why.R;

import java.util.List;

public class DialogUtil {
	/**
	 * 加载中dialog
	 */
	private static Dialog loadingDialog;

	public static void showLoading(Activity activity, String msg, boolean showInBottom) {
		View loadingView = View.inflate(activity, R.layout.dialog_loading_custom, null);
		if(!TextUtils.isEmpty(msg)){
			((TextView) loadingView.findViewById(R.id.tip)).setText(msg);
		}
		Dialog dialog = new Dialog(activity, R.style.loadingDialog);
		dialog.setCancelable(true);
		dialog.setCanceledOnTouchOutside(false);
		dialog.setContentView(loadingView);
		Window window = dialog.getWindow();
		WindowManager.LayoutParams lp = window.getAttributes();
		lp.width = DensityUtils.dip2px(activity, 120);
		lp.height = DensityUtils.dip2px(activity, 120);
		lp.x = 0;

		if (showInBottom) {
			lp.y = DensityUtils.dip2px(activity, 80);
			lp.gravity = Gravity.BOTTOM;
		}

		window.setAttributes(lp);
		dialog.show();

		if (loadingDialog != null && loadingDialog.isShowing()) {
			loadingDialog.dismiss();
		}

		loadingDialog = dialog;
	}

	public static void dismissLoading() {
		if (loadingDialog != null && loadingDialog.isShowing()) {
			loadingDialog.dismiss();
		}
	}

	/**
	 * 列表菜单弹框
	 */
	public static class CenterMenuBuidler {
		private Context context;
		private Dialog dialog;

		private Button btnCancel;
		private ViewGroup layContainer;

		public static CenterMenuBuidler init(Context context) {
			CenterMenuBuidler cb = new CenterMenuBuidler();
			cb.context = context;
			return cb;
		}

		public CenterMenuBuidler create(List<Menu> menuList, boolean canCancel, boolean shadow) {
			dialog = new Dialog(context, shadow ? R.style.Theme_Light_NoTitle_Dialog : R.style.Theme_Light_NoTitle_NoShadow_Dialog);
			Window window = dialog.getWindow();

			window.getDecorView().setPadding(0, 0, 0, 0);
			WindowManager.LayoutParams lp = window.getAttributes();
			lp.width = WindowManager.LayoutParams.MATCH_PARENT;
			lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
			window.setAttributes(lp);
//			window.setGravity(Gravity.BOTTOM);

			View view = LayoutInflater.from(context).inflate(R.layout.dialog_center_menu, null);

			btnCancel = (Button) view.findViewById(R.id.btn_cancel);
			btnCancel.setVisibility(View.GONE);
			layContainer = (ViewGroup) view.findViewById(R.id.lay_container);
			LayoutParams lpItem = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
			LayoutParams lpDevider = new LayoutParams(LayoutParams.MATCH_PARENT, 1);
			int spacing = BaseUtils.dipToPx(10);
			for (int i = 0; i < menuList.size(); i++) {
				Menu bottomMenu = menuList.get(i);
				Button bbm = new Button(context);
				bbm.setLayoutParams(lpItem);
				int backgroundResId = R.drawable.menu_item_single;
				if (menuList.size() > 1) {
					if (i == 0) {
						backgroundResId = R.drawable.menu_item_top;
					}
					else if (i == menuList.size() - 1) {
						backgroundResId = R.drawable.menu_item_bottom;
					} 
					else {
						backgroundResId = R.drawable.menu_item_middle;
					}
				}
				bbm.setBackgroundResource(backgroundResId);
				bbm.setPadding(spacing * 2, (int) (spacing * 1.5f), spacing * 2,  (int) (spacing * 1.5f));
				bbm.setGravity(Gravity.LEFT);
				bbm.setText(bottomMenu.funName);
				bbm.setTextColor(0xff3d3d3d); // 0xff007aff - 蓝色
				bbm.setTextSize(19);
				bbm.setOnClickListener(bottomMenu.listener);
				layContainer.addView(bbm);

				if (i != menuList.size() - 1) {
					View viewDevider = new View(context);
					viewDevider.setLayoutParams(lpDevider);
					viewDevider.setBackgroundColor(0xffcccccc);
					layContainer.addView(viewDevider);
				}
			}

			dialog.setContentView(view);
			dialog.setCanceledOnTouchOutside(canCancel);
			dialog.setCancelable(canCancel);
			return this;
		}

		public CenterMenuBuidler setCancelListener(OnClickListener cancelListener) {
			btnCancel.setVisibility(View.VISIBLE);
			btnCancel.setOnClickListener(cancelListener);
			return this;
		}

		public CenterMenuBuidler setCancelText(int resId) {
			btnCancel.setText(resId);
			return this;
		}

		public CenterMenuBuidler setCancelText(String text) {
			btnCancel.setText(text);
			return this;
		}

		public Dialog show() {
			dialog.show();
			return dialog;
		}

		public void dismiss() {
			if (null != dialog && dialog.isShowing()) {
				dialog.dismiss();
			}
		}

		public Dialog complete() {
			return dialog;
		}

		public static class Menu {
			public String funName;
			public OnClickListener listener;

			public Menu(String funName, OnClickListener listener) {
				this.funName = funName;
				this.listener = listener;
			}
		}
	}
}
